let tab = document.getElementsByClassName('tab')[0],
    tabItems = tab.getElementsByTagName('li'),
    underline = tab.getElementsByClassName('underline')[0],
    emailLoginPage = document.getElementsByClassName('email')[0],
    usernameLoginPage = document.getElementsByClassName('username')[0],
    registerPage = document.getElementsByClassName('register')[0],
    emailLoginBtn = document.getElementsByTagName('button')[0],
    usernameLoginBtn = document.getElementsByTagName('button')[1],
    registerBtn = document.getElementsByTagName('button')[2];
tab.addEventListener('click',(event) => {
  if(event.target.innerText == '邮箱登录') {
    underline.style.left = '0';
    underline.style.width = '72px';
    registerPage.className = 'register';
    emailLoginPage.className = 'login email active';
    usernameLoginPage.className = 'login username';
  }else if(event.target.innerText == '注册账号') {
    underline.style.left = '200px';
    underline.style.width = '72px';
    registerPage.className = 'register active';
    emailLoginPage.className = 'login email';
    usernameLoginPage.className = 'login username';
  }else if(event.target.innerText == '用户名登录'){
    underline.style.left = '90px';
    underline.style.width = '90px';
    registerPage.className = 'register';
    emailLoginPage.className = 'login email';
    usernameLoginPage.className = 'login username active';
  }
});

function login(method) {
  let loginData;
  let checkRes;
  if(method == 'email') {
    loginData = {
      email: emailLoginForm.email.value,
      password: emailLoginForm.password.value,
    }
    disableBtn('email');
    checkRes = checkFormat(loginData.email, undefined, loginData.password);
  }else if(method == 'username') {
    loginData = {
      username: usernameLoginForm.email.value,
      password: usernameLoginForm.password.value,
    }
    disableBtn('username');
    checkRes = checkFormat(undefined, loginData.username, loginData.password);
  }
  if(checkRes){
    request('http://175.178.51.126:8091/smallA/login', 'POST', JSON.stringify(loginData))
    .then(value => {
      if(value.code == 200) {
        if((emailLoginForm.save.checked && method == 'email') || (usernameLoginForm.save.checked && method == 'username')) {
          for(let key in value.data) {
            sessionStorage.setItem(key, value.data[key]);
            setCookie(key, value.data[key], 7);
          }
        }else {
          for(let key in value.data) {
            sessionStorage.setItem(key, value.data[key]);
          }
        }
        alert('登录成功！');
        location.href = 'index.html';
      }else{
        alert(value.msg);
        recoverBtn();
      }
    })
    .catch(error => {
      console.log(error);
      recoverBtn();
    })
  }else {
    recoverBtn();
  }
}
function register() {
  const registerData = {
    email: registerForm.email.value,
    username: registerForm.username.value,
    password: registerForm.password.value
  }
  registerBtn.setAttribute('disabled','disabled');
  let checkRes = checkFormat(registerData.email, registerData.username, registerData.password, registerForm.passwordConfirm.value);
  if(checkRes != false) {
    request('http://175.178.51.126:8091/smallA/register', 'POST', JSON.stringify(registerData))
    .then(value => {
      if(value.code == 200) {
        emailLoginForm.email.value = registerData.email;
        emailLoginForm.password.value = registerData.password;
        login('email');
      }else{
        alert(value.msg);
        registerBtn.removeAttribute('disabled');
      }
    })
    .catch(error => {
      console.log(error);
      registerBtn.removeAttribute('disabled');
    })
  }else {
    registerBtn.removeAttribute('disabled');
  }
}
function checkFormat(email, username, password, passwordConfirm) {
  if(email != undefined) {
    if(email == '') {
      alert('邮箱为空！');
      return false;
    }else if (!/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(email)) {
      alert('邮箱格式不对！');
      return false;
    }
  }
  if(username != undefined && username == '') {
    alert('用户名为空！');
    return false
  }
  if(password != undefined && password == '') {
    alert('密码为空！');
    return false;
  }
  if(passwordConfirm != undefined) {
    if(passwordConfirm == '') {
      alert('请再次输入密码！');
      return false;
    }else if(passwordConfirm != password) {
      alert('两次输入的密码不一致！');
      return false;
    }
  }
  return true;
}
function recoverBtn() {
  emailLoginBtn.removeAttribute('disabled');
  emailLoginBtn.className = 'primary full';
  usernameLoginBtn.removeAttribute('disabled');
  usernameLoginBtn.className = 'primary full';
}
function disableBtn(content) {
  if(content == 'email') {
    emailLoginBtn.setAttribute('disabled','disabled');
    emailLoginBtn.className = 'primary full disabled';
  }else if(content == 'username') {
    usernameLoginBtn.setAttribute('disabled','disabled');
    usernameLoginBtn.className = 'primary full disabled';
  }
}
function changeForgetPage(operation) {
  let page = document.getElementsByClassName('layer')[0];
  if(operation == 'open') {
    page.style.display = 'block';
  }else if(operation == 'close') {
    page.style.display = 'none';
  }
}